import { defineStore } from 'pinia'
import { StoreType } from './type'
import type { IUserEntity } from '@/types'
import request from '@/service'
import { cache } from '@/utils'

interface IUserState {
  userInfo: IUserEntity
  permission: string[]
  columnPageFilter: any
}

export const useUserStore = defineStore(StoreType.USER_INFO, {
  state: (): IUserState => {
    return {
      userInfo: {} as IUserEntity,
      permission: [],
      columnPageFilter: {}
    }
  },
  actions: {
    getByKey(key: keyof IUserState) {
      return this[key]
    },

    setByKey(key: keyof IUserState, data: any, localKey = '') {
      this[key] = data
      if (localKey) cache.set(localKey, data)
    },

    /**
     * 设置页面列表筛选，local存储
     * @param key
     * @param data
     */
    setColumnPageFilter(key: string, data: any) {
      this.columnPageFilter[key] = data
      cache.set('columnPageFilter', this.columnPageFilter)
    },

    /**获取用户信息 */
    async getUserInfo() {
      return request.get<IUserEntity>('/auth/getUserInfo')
    },

    /**存储用户信息 */
    setUserInfo(userInfo: IUserEntity) {
      this.userInfo = userInfo
    }
  }
})
